Import("env")
# import os
# import shutil
# env.Replace(PROGNAME="firmware_%s" % env.GetProjectOption("custom_prog_version"))
env.Append(LINKFLAGS=["--specs=nosys.specs", "--specs=nano.specs"])

env.AddPostAction(
    "$BUILD_DIR/${PROGNAME}.elf",
    env.VerboseAction(" ".join([
        "$OBJCOPY", "-O", "ihex", "-R", ".eeprom",
        "$BUILD_DIR/${PROGNAME}.elf", "$BUILD_DIR/${PROGNAME}.hex"
    ]), "Building $BUILD_DIR/${PROGNAME}.hex")
)
# new_file_name = env.GetProjectOption("custom_prog_name") + "_main" \
#      + "_V" + env.GetProjectOption("custom_firmware_version_major") + "." \
#      + env.GetProjectOption("custom_firmware_version_minor") + "." \
#      + env.GetProjectOption("custom_firmware_version_revision")

# env.AddPostAction(
#     "$BUILD_DIR/${PROGNAME}.elf",
#     env.VerboseAction(" ".join([
#         "$OBJCOPY", "-O", "ihex", "-R", ".eeprom",
#         "$BUILD_DIR/${PROGNAME}.elf", "$BUILD_DIR/" + new_file_name + ".hex"
#     ]), "Building $BUILD_DIR/" + new_file_name + ".hex")
# )

# env.AddPostAction(
#     "$BUILD_DIR/${PROGNAME}.elf",
#     env.VerboseAction(" ".join([
#         "$OBJCOPY", "-O", "binary",
#         "$BUILD_DIR/${PROGNAME}.elf", "$BUILD_DIR/" + new_file_name + ".bin"
#     ]), "Building $BUILD_DIR/" + new_file_name + ".bin")
# )

#print(env.get("PROGNAME", "no_name"))

# path = env.get("PROJECT_BUILD_DIR")
# sub_folder_path = env.get("PIOENV")
# old_file_name = env.get("PROGNAME")

# old_name = (path + "\\" + sub_folder_path + "\\" + old_file_name + ".bin")
# new_name = (path + "\\" + sub_folder_path + "\\" + new_file_name )
# old_name = (path + "\\" + sub_folder_path + "\\" + old_file_name + ".bin").replace('\\', '/')
# new_name = (path + "\\" + sub_folder_path + "\\" + new_file_name + "_main"  + "_V" + env.GetProjectOption("custom_firmware_version_major") + "." + env.GetProjectOption("custom_firmware_version_minor") + "." + env.GetProjectOption("custom_firmware_version_revision") + ".bin").replace('\\', '/')

# print(old_name)
# print(new_name)
# try:
# shutil.copy(old_name, new_name)
# except WindowsError:
#     os.remove(new_name)
#     os.rename(old_name, new_name)


